//
//  YDCustomerListController.m
//  YDTestDrive
//
//  Created by ios on 2017/2/9.
//  Copyright © 2017年 ios. All rights reserved.
//

#import "YDCustomerListController.h"
#import "YDCustomerDetailController.h"
#import "YDCustomerSearchController.h"

#import "YDCustomerListCell.h"
#import "YDNoDataView.h"

#import "YDCustomerListModel.h"

@interface YDCustomerListController () <UITableViewDataSource,UITableViewDelegate,UISearchControllerDelegate,UISearchBarDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *customerArray;
@property (nonatomic, strong) YDNoDataView *noDataView;

@property (nonatomic, strong) UISearchController *searchController; //搜索控制器
@property (nonatomic, strong) YDCustomerSearchController *searchResultController; //搜索结果控制器

@end

@implementation YDCustomerListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"试驾客户";
    
    _customerArray = [NSMutableArray array];
    
    [self createView];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self loadDataFormNetwork];
}

- (void)createView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64.0f - 49.0f) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib(@"YDCustomerListCell")];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = kViewControllerBackgroundColor;
    [self.view addSubview:_tableView];
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadDataFormNetwork];
    }];
    _tableView.mj_header.backgroundColor = kViewControllerBackgroundColor;
    
    _noDataView = [YDNoDataView noDataView];
    _noDataView.frame = _tableView.bounds;
    _noDataView.imageView.image = [UIImage imageNamed:@"blank_sj"];
    _noDataView.titleLabel.text = @"   暂无试驾客户!";
    _noDataView.hidden = YES;
    [_tableView addSubview:_noDataView];
    [_tableView bringSubviewToFront:_noDataView];
    
    _tableView.tableHeaderView = self.searchController.searchBar;
    //这三个属性和搜索栏有关
    self.definesPresentationContext = YES;
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
    [self setExtendedLayoutIncludesOpaqueBars:YES];
}

#pragma mark - 网络
- (void)loadDataFormNetwork
{
    NSDictionary *dic = @{};
    NSString *URL = [NSString stringWithFormat:@"%@sid=%@", kCustomerListAddress, getNSUser(kAPP_SID)];
    
    //[MBProgressHUD showStatus:@"" toView:self.view];
    [YDDataService POST:dic url:URL block:^(id result) {
        
        if ([[result valueForKey:@"code"] isEqualToString:@"S_OK"]) {
            NSArray *custArray = [result valueForKey:@"var"];
            [self updateTableViewWithCustomerArray:custArray];
        }
        
        //[MBProgressHUD hideHUDForView:self.view animated:YES];
        [_tableView.mj_header endRefreshing];
    } failBlock:^(id error) {
        //[MBProgressHUD hideHUDForView:self.view animated:YES];
        [_tableView.mj_header endRefreshing];
    }];
}

- (void)updateTableViewWithCustomerArray:(NSArray *)custArray
{
    [self.customerArray removeAllObjects];
    
    for (NSDictionary *dic in custArray) {
        YDCustomerListModel *model = [YDCustomerListModel instanceWithDict:dic];
        [self.customerArray addObject:model];
    }
    [self.tableView reloadData];
    
    if (self.customerArray.count > 0) {
        self.noDataView.hidden = YES;
    } else {
        self.noDataView.hidden = NO;
    }
}


#pragma mark - tableView delegete
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.customerArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDCustomerListModel *model = self.customerArray[indexPath.row];
    YDCustomerListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YDCustomerListCell"];
    cell.customerModel = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YDCustomerListModel *model = self.customerArray[indexPath.row];
    YDCustomerDetailController *VC = [[YDCustomerDetailController alloc] init];
    VC.customerListModel = model;
    [self.navigationController pushViewController:VC animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 74.0f;
}


#pragma mark - 搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length > 0)
    {
        self.searchResultController.searchKey = searchText;
    }
}

- (UISearchController *)searchController
{
    if (_searchController == nil) {
        _searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultController];
        [_searchController.searchBar sizeToFit];
        _searchController.searchBar.delegate = self;
        _searchController.searchBar.placeholder = @"输入客户姓名或手机号";
        _searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
        [_searchController.searchBar.layer setBorderWidth:0];
        _searchController.searchBar.barTintColor = kViewControllerBackgroundColor;
        [_searchController.searchBar setBackgroundImage:[UIImage new]];
        [_searchController.searchBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
        _searchController.view.backgroundColor = kViewControllerBackgroundColor;
        
        [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"取消"];
    }
    return _searchController;
}

- (YDCustomerSearchController *)searchResultController
{
    if (_searchResultController == nil) {
        _searchResultController = [[YDCustomerSearchController alloc] init];
        WEAKSELF
        _searchResultController.SelectModelBlock = ^(YDCustomerListModel *selectModel) {
            YDCustomerDetailController *VC = [[YDCustomerDetailController alloc] init];
            VC.customerListModel = selectModel;
            [weakSelf.navigationController pushViewController:VC animated:YES];
        };
    }
    return _searchResultController;
}

@end
